//
//  WLTabBarController.m
//  WLTabBar
//
//  Created by tanxl on 2019/12/29.
//  Copyright © 2019 tanxl. All rights reserved.
//

#import "WLTabBarController.h"
#import "UIImage+Color.h"

@interface WLTabBarController ()

@end

@implementation WLTabBarController

- (void)loadView {
    [super loadView];
    
    WLTabBar *tabBar = [[WLTabBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, kTabBarHeight)];
    tabBar.delegate  = self;
    
    self.WLTabBar = tabBar;
    [self.tabBar addSubview:tabBar];
//    self.tabBar.backgroundColor = [UIColor clearColor];

    [self.tabBar setTranslucent:NO];
    [self.tabBar setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor]];
    [self.tabBar setShadowImage:[UIImage imageWithColor:[UIColor colorWithWhite:.5f alpha:.1f]]];
    
//    self.tabBar.layer.shadowOpacity = 0.2f;
//    self.tabBar.layer.shadowRadius = 5.f;
//    self.tabBar.layer.shadowOffset = CGSizeMake(0,0);
//    self.tabBar.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.3f].CGColor;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideOriginControls) name:kGTNotificationTabBarItemChanged object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self hideOriginControls];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self hideOriginControls];
}

- (void)hideOriginControls {
    [self.tabBar.subviews enumerateObjectsUsingBlock:^(__kindof UIView * obj, NSUInteger idx, BOOL * stop) {
        if ([obj isKindOfClass:[UIControl class]]) {
            [obj setHidden:YES];
        }
    }];
}

// 1.赋值nav的方式只能选[setViewControllers:]
- (void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers{
    
    [self.WLTabBar.tabBarItems makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.WLTabBar.tabBarItems = [NSMutableArray array];
    
    NSInteger index = 0;
    for (UINavigationController *nav in viewControllers) {
        // 2.为了启动KVO,赋值和关联的tabBarItem需要相同
        NSString *title = nav.tabBarItem.title;
        nav.tabBarItem.title = title;
        [self.WLTabBar addTabBarItem:nav.tabBarItem index:index isEnd:(index == (viewControllers.count-1))];
        index ++;
    }
    
    [super setViewControllers:viewControllers];
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    [super setSelectedIndex:selectedIndex];
    
    self.WLTabBar.selectedItem.selected = NO;
    self.WLTabBar.selectedItem = self.WLTabBar.tabBarItems[selectedIndex];
    self.WLTabBar.selectedItem.selected = YES;
}

#pragma mark - XXTabBarDelegate Method
- (BOOL)tabBar:(WLTabBar *)tabBarView didSelectedItemFrom:(NSInteger)from to:(NSInteger)to {
    self.selectedIndex = to;
    return true;
}


- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    CGRect frame = self.tabBar.frame;
    frame.size.height = kTabBarHeight + kBottomSafeHeight;
    frame.origin.y = self.view.frame.size.height - frame.size.height;
    self.tabBar.frame = frame;
    self.tabBar.barStyle = UIBarStyleBlack;
}
@end
